package com.wangzhenyu.learn.test.android_.adapter_

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.wangzhenyu.learn.R
import com.wangzhenyu.common.util.log.LogUtil


@Suppress("UNUSED")
class SimpleListAdapter(private val data: List<String>, private val function: FragmentForClick) :
    RecyclerView.Adapter<SimpleListAdapter.SimpleListViewHolder>() {

    inner class SimpleListViewHolder(view: View) : RecyclerView.ViewHolder(view),
        View.OnClickListener {

        private var content: String? = null
        private val textView: TextView = view.findViewById(R.id.tv)

        init {
            textView.setOnClickListener(this)
        }

        override fun onClick(v: View?) {

            function.onRecyclerViewClick(layoutPosition)
        }

        fun bind(content: String) {
            this.content = content
            textView.text = content
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SimpleListViewHolder =
        SimpleListViewHolder(
            LayoutInflater.from(parent.context).inflate(R.layout.test_item_test02, parent, false)
        )

    override fun onBindViewHolder(holder: SimpleListViewHolder, position: Int) {
        holder.bind(data[position])
    }

    override fun getItemCount() = data.size

}

interface FragmentForClick {
    fun onRecyclerViewClick(position: Int)
}